﻿using UnityEngine;
using UnityEngine.EventSystems;

public class CameraCtl : MonoBehaviour
{
    public float speedX = 10.0f;
    public float speedZ = 20.0f;
    public float speedF = 50.0f;
    public float speedR = 40.0f;
    /// <summary>
    /// 使用此变量就算相机绕Y轴旋转180度按下前方向键依然朝着正面移动
    /// </summary>
    public Transform myForward;

    //bool enableRot=true;
   
    void Update ()
	{
	    CameraRota();
	    CameraMove();
	}
    private void CameraRota()
    {
        
        if (EventSystem.current.IsPointerOverGameObject()) return;

        if (Input.GetMouseButton(0)&&EventSystem.current.IsPointerOverGameObject()==false)
        {
            float h = Input.GetAxis("Mouse X");
            float v = Input.GetAxis("Mouse Y");
            if (h != 0)
            { 
                transform.RotateAround(transform.position, Vector3.up * h, speedR * Time.deltaTime);
            }
            if (v != 0)
            {
                transform.RotateAround(transform.position, -transform.right * v, speedR * Time.deltaTime);
            } 
        } 
    }
    private void CameraMove()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float m = Input.GetAxis("Mouse ScrollWheel"); 

        if (h != 0)
        {
            transform.Translate(Vector3.right * h * Time.deltaTime * speedX);
        }
        if (v != 0)
        {  
            transform.Translate(myForward.forward * v * Time.deltaTime * speedZ, Space.World); 
        }
        if (m != 0)
        {
            transform.Translate(transform.forward * m * Time.deltaTime * speedF, Space.World);
        }

        if (transform.position.y <= 2.0f)
        {
            transform.position = new Vector3(transform.position.x, 2.0f, transform.position.z);
        }
    }
}